//
//  MatchViewController.m
//  礼物go
//
//  Created by dllo on 16/5/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MatchViewController.h"
#import "SelectTableViewCell.h"
#import "SelectContentModel.h"
#import "SelectDetailViewController.h"
#import "LoginViewController.h"


static NSString *const matchCell = @"matchCell";

@interface MatchViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *matchTableView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,assign)NSInteger nextPage;//记录下一页
@property(nonatomic,assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉

@end

@implementation MatchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArray = [NSMutableArray array];
    
    self.matchTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, self.view.frame.size.height - 140) style:UITableViewStylePlain];
    [self.view addSubview:self.matchTableView];
    self.matchTableView.dataSource = self;
    self.matchTableView.delegate = self;
    [self.matchTableView registerClass:[SelectTableViewCell class] forCellReuseIdentifier:matchCell];
    self.matchTableView.showsVerticalScrollIndicator = NO;
    
    self.isUpLoading = NO;
 
    
    [self addHeader];
    [self addFooter];

    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.matchTableView reloadData];

}


-(void)setDataId:(NSString *)dataId
{
    _dataId =dataId;
    [self getSelectList:0];

}

#pragma mark --下拉刷新
-(void)addHeader{
  
    __block MatchViewController *matchVC = self;
    // 添加上拉刷新尾部控件
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        matchVC.nextPage = 0;//记录页码
        matchVC.isUpLoading = NO;//标记为下拉操作
        [matchVC getSelectList:matchVC.nextPage];//重新请求数据
    }];
#pragma mark 自动刷新(一进入程序就下拉刷新)
  //[header beginRefreshing];
    
    self.matchTableView.mj_header = header;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 1; i < 6; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"box_0%d",i]];
        [imageArray addObject:image];
    }
    [header setImages:imageArray forState:MJRefreshStateRefreshing];
    
    UIImage *pullImage = [imageArray firstObject];
    NSArray *pullArray = [NSArray arrayWithObjects:pullImage, nil];
    [header setImages:pullArray forState:MJRefreshStatePulling];
    
}

#pragma mark --上拉加载更多
-(void)addFooter{
    __block MatchViewController *matchVC = self;
    // 添加上拉刷新尾部控件
    self.matchTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        matchVC.nextPage ++;
        matchVC.isUpLoading = YES;//标记为上拉操作
        [matchVC getSelectList:matchVC.nextPage];//请求数据
    }];
}

-(void)getSelectList:(NSUInteger)nextCursor{
   
    AFNetworkReachabilityManager *netWorkManger = [AFNetworkReachabilityManager sharedManager];
    //****参数cursor的值: 第一页默认为0，下一页就是请求下来数据中next_cursor的值
    NSString *urlStr = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?gender=2&generation=4&limit=20&offset=%ld",self.dataId, 20 * nextCursor];
    
 [LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
     [netWorkManger stopMonitoring];
     if (self.isUpLoading == NO) {
         //说明是下拉，就要清空数组中的数据
         [self.dataArray removeAllObjects];
     }
     NSDictionary *dataDic = [result objectForKey:@"data"];
     NSArray *itemArray = [dataDic objectForKey:@"items"];
     for (NSDictionary *dic in itemArray) {
         SelectContentModel *contentModel = [[SelectContentModel alloc] init];
         [contentModel setValuesForKeysWithDictionary:dic];
         [self.dataArray addObject:contentModel];
     }
     
     [self.matchTableView.mj_header endRefreshing];
     [self.matchTableView.mj_footer endRefreshing];
     
     [self.matchTableView reloadData];
     
 } error:^(NSError *error) {
     
     
 }];
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:matchCell];
    SelectContentModel *select = [self.dataArray objectAtIndex:indexPath.row];
    cell.selectContent = select;
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    __block MatchViewController *matchVC = self;
    cell.likePush = ^(){
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [matchVC presentViewController:loginVC animated:YES completion:^{
            
            
        }];
    };

    
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectContentModel *selectModel = [self.dataArray objectAtIndex:indexPath.row];
    SelectDetailViewController *SDVC = [[SelectDetailViewController alloc] init];
    SDVC.dataId = selectModel.myId;
    SDVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:SDVC animated:YES];


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
